/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commonInfrastructure.accountManagement.managedbean;

import commonInfrastructure.accountManagement.entity.RoleEntity;
import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.accountManagement.session.PasswordHashSessionBean;
import commonInfrastructure.accountManagement.session.RoleSessionBean;
import commonInfrastructure.accountManagement.session.SalesmanSessionBean;
import java.io.IOException;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Wei Haoyang
 */
@ManagedBean
@ViewScoped
public final class AddSalesmanManagedBean implements Serializable {

    /** Creates a new instance of AddEmployeeManagedBean */
    @EJB
    private SalesmanSessionBean salesmanManager;
    @EJB
    private RoleSessionBean roleManager;
    @EJB
    private PasswordHashSessionBean passwordHashSessionBean;
    private SalesmanEntity salesman;
    private String stringRoles;
    private List<RoleEntity> allRoles;
    private String[] names;
    private String[] selected;
    private List<String> selectedRoles;
    private boolean show;
    private String dl;
    private String al;

    public AddSalesmanManagedBean() {
        salesman = new SalesmanEntity();
        selectedRoles = new ArrayList<String>();
    }

    public void saveNewSalesman(ActionEvent event) throws IOException {
        for (String roleId : selectedRoles) {
            System.out.println("********* " + roleId);
        }

        /*Collection<Role> roles = retrieveRoles(tokenizeSentence(stringRoles));
        
        getEmployee().setRoles(roles);
         */

        pushToRoles();
        System.out.print("***********************" + salesman.getPassword());

        setLevel();
        salesman.setPassword(passwordHashSessionBean.hashPassword(salesman.getPassword()));
        System.out.print("***********************" + salesman.getPassword());
        try{
        salesmanManager.addSalesman(salesman);
        }catch(Exception e){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "User Name Already Exists", ""));
            return;
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "New Salesman saved.", ""));
        /*FacesContext.getCurrentInstance().getExternalContext().redirect("debug.xhtml");
        /*try {
        Thread.currentThread().sleep(3000);
        
        
        } catch (InterruptedException e) {
        }
        
        FacesContext.getCurrentInstance().getExternalContext().redirect("AddEmployee.xhtml");
         */
        setShow(true);
    }

    public void setLevel() {
        int lv = Integer.valueOf(salesman.getSalesManLevel());
        switch (lv) {
                case 1: 
                    salesman.setAmountLimit(50000);
                    salesman.setDiscountLimit(0.95);
                    break;
                case 2: 
                    salesman.setAmountLimit(80000);
                    salesman.setDiscountLimit(0.93);
                    break;
                case 3: 
                    salesman.setAmountLimit(100000);
                    salesman.setDiscountLimit(0.92);
                    break;
                case 4: 
                    salesman.setAmountLimit(999999999);
                    salesman.setDiscountLimit(0.90);
                    break;
        }
    }

    public void oneMore(ActionEvent event) throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().redirect("AddSalesman.xhtml");
    }

    public SalesmanEntity getSalesman() {
        return salesman;
    }

    public void setSalesman(SalesmanEntity salesman) {
        this.salesman = salesman;
    }

    /*    public static String[] tokenizeSentence(String str) {
    StringTokenizer tokenizer = new StringTokenizer(str, ",");
    List tempList = new ArrayList();
    while (tokenizer.hasMoreElements()) {
    tempList.add(tokenizer.nextElement());
    }
    String[] ret = new String[tempList.size()];
    tempList.toArray(ret);
    return ret;
    }
    
    public Collection<Role> retrieveRoles(String[] str){
    Collection<Role> roles = new ArrayList<Role>();
    int i = 0;
    RoleEntity role;
    
    do{
    role = roleManager.getRoleByName(str[i]);
    roles.add(role);
    }while(str[i] != null);
    
    return roles;
    }
    
    /**
     * @return the stringRoles
     */
    public String getStringRoles() {
        return stringRoles;
    }

    /**
     * @param stringRoles the stringRoles to set
     */
    public void setStringRoles(String stringRoles) {
        this.stringRoles = stringRoles;
    }

    /**
     * @return the allRoles
     */
    public List<RoleEntity> getAllRoles() {
        return allRoles;
    }

    /**
     * @param allRoles the allRoles to set
     */
    public void setAllRoles(List<RoleEntity> allRoles) {
        this.setAllRoles(allRoles);
    }

    /**
     * @return the employeeManager
     */
    public SalesmanSessionBean getSalesmanManager() {
        return salesmanManager;
    }

    /**
     * @param employeeManager the employeeManager to set
     */
    public void setSalesmanManager(SalesmanSessionBean salesmanManager) {
        this.salesmanManager = salesmanManager;
    }

    /**
     * @return the roleManager
     */
    public RoleSessionBean getRoleManager() {
        return roleManager;
    }

    /**
     * @param roleManager the roleManager to set
     */
    public void setRoleManager(RoleSessionBean roleManager) {
        this.roleManager = roleManager;
    }

    public void retrieveNames(List<RoleEntity> roles) {
        int i = 0;
        while (roles.get(i) != null) {
            getNames()[i] = roles.get(i).getRoleName();
            i++;
        }
    }

    /**
     * @return the names
     */
    public String[] getNames() {
        return names;
    }

    /**
     * @param names the names to set
     */
    public void setNames(String[] names) {
        this.names = names;
    }

    /**
     * @return the selected
     */
    public String[] getSelected() {
        return selected;
    }

    /**
     * @param selected the selected to set
     */
    public void setSelected(String[] selected) {
        this.selected = selected;
    }

    public List<String> getSelectedRoles() {
        return selectedRoles;
    }

    public void setSelectedRoles(List<String> selectedRoles) {
        this.selectedRoles = selectedRoles;
    }

    public void pushToRoles() {
        int i = 0;
        Long id;
        id = Long.valueOf(selectedRoles.get(i));
        salesman.getRoles().add(roleManager.getRole(id));
        System.out.println(selectedRoles.get(i));

        while (i < (selectedRoles.size() - 1)) {
            i++;
            id = Long.valueOf(selectedRoles.get(i));
            salesman.getRoles().add(roleManager.getRole(id));
            System.out.println(selectedRoles.get(i));
        }

    }

    /**
     * @return the show
     */
    public boolean isShow() {
        return show;
    }

    /**
     * @param show the show to set
     */
    public void setShow(boolean show) {
        this.show = show;
    }

    public String getDl() {
        return dl;
    }

    public void setDl(String dl) {
        this.dl = dl;
    }

    public String getAl() {
        return al;
    }

    public void setAl(String al) {
        this.al = al;
    }
}
